package com.ch.home.service.serviceImpl.gameServiceImpl;

import com.ch.home.bean.GameRepository;
import com.ch.home.mapper.gameMapper.GameRepositoryMapper;
import com.ch.home.service.serviceInterface.gameSerivce.GameRepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
 * Created by CHEN on 2019/8/29.
 */
@Service
public class GameRepositoryServiceImpl implements GameRepositoryService {

    @Autowired
    GameRepositoryMapper gameRepositoryMapper;


    public static ArrayList<Integer> questions = new ArrayList<>( );


    @Override
    public HashMap getRandomQuestion() {

        HashMap<Object, Object> result = new HashMap( );

        Integer questionId;
        if (questions.size( ) == 0) {
            questions = this.getAllQuestionId( );
        }
        Random random = new Random( );
        int i = random.nextInt(questions.size( ));

        System.out.println(i);
        questionId = questions.get(i);

        System.out.println(questionId);

        ArrayList<GameRepository> questionByCondition = gameRepositoryMapper.getQuestionByCondition(new GameRepository(questionId, null, null, null, null, null));

        if (questionByCondition.size( ) == 1) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }
        result.put("value", questionByCondition);
        return result;
    }

    @Override
    public ArrayList<Integer> getAllQuestionId() {
        ArrayList<Integer> allQuestionId = gameRepositoryMapper.getAllQuestionId( );
        return allQuestionId;
    }

    @Override
    public HashMap addQuestion(GameRepository gameRepository) {
        HashMap<Object, Object> result = new HashMap( );


        Integer re = gameRepositoryMapper.addQuestion(gameRepository);
        if (re == 1) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }
        return result;
    }

    @Override
    public HashMap addMulQuestion(ArrayList<GameRepository> list) {

        HashMap<Object, Object> result = new HashMap( );
        Integer re = gameRepositoryMapper.addMulQuestion(list);

        if (re == list.size( )) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }
        return result;
    }


    public HashMap transferAndAdd(ArrayList<String> questionUrl, ArrayList<String> questionContent, ArrayList<String> option1, ArrayList<String> option2, ArrayList<String> answer) {
        HashMap<Object, Object> result = new HashMap( );
        ArrayList<GameRepository> gameRepositories = new ArrayList<>( );

        for (int i = 0; i < questionContent.size( ); i++) {
            if (questionUrl.get(i).equals("")) questionUrl.set(i, null);
            if (questionContent.get(i).equals("")) {
                result.put("message", "questionContent[" + i + "] is null");
                return result;
            }
            if (option1.get(i).equals("")) {
                result.put("message", "option1[" + i + "] is null");
                return result;
            }
            if (option2.get(i).equals("")) {
                result.put("message", "option2[" + i + "] is null");
                return result;
            }
            if (answer.get(i).equals("")) {
                result.put("message", "answer[" + i + "] is null");
                return result;
            }
            gameRepositories.add(new GameRepository(null, questionUrl.get(i), questionContent.get(i), option1.get(i), option2.get(i), answer.get(i)));
        }

        System.out.println(gameRepositories);
        if (gameRepositories.size( ) >= 1) result = this.addMulQuestion(gameRepositories);
        else result.put("message", "fail");
        result.put("value", gameRepositories);

        return result;
    }


    @Override
    public HashMap UpdateQuestion(GameRepository gameRepository) {

        HashMap<Object, Object> result = new HashMap( );
        if (gameRepository.getQuestionId( ).equals("") || gameRepository.getQuestionId( ) == null) {
            result.put("message", "fail");
            return result;
        }
        Integer re = gameRepositoryMapper.updateQuestion(gameRepository);
        if (re == 1) {
            result.put("message", "success");
        } else {
            result.put("message", "fail");
        }
        return result;
    }
}
